﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using FPC.Offer.Common;
namespace FPC.Offer.UI.GeneralProcessor.Systems
{
    /// <summary>
    /// HandlerDictionary 的摘要说明
    /// </summary>
    public class HandlerDictionary : IHttpHandler
    {
        FPC.Offer.Service.System_DictionaryTableService s = new FPC.Offer.Service.System_DictionaryTableService();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var Actioon = context.Request["Actioon"].ToString();
            Model.System_DictionaryTable mo = new Model.System_DictionaryTable();
            //查询
            if (Actioon == EnumAction.Select.ToString())
            {

                if (context.Request["context"] != null)
                {
                    if (context.Request["context"].ToString() == "Sysyem_Data")
                    {
                        context.Response.Write(JsonConvert.SerializeObject(s.SelectGroupBy()));
                    }
                    else 
                    {
                        var PlaceField = context.Request["context"].ToString();
                        if (PlaceField != "")
                        {
                            context.Response.Write(JsonConvert.SerializeObject(s.Find(p => p.PlaceField == PlaceField)));
                        }
                    }                
                }
                else if (context.Request["GetName"] != null)
                {
                    var GetName = context.Request["GetName"].ToString();
                    if (GetName == "FPC_Offer")
                    {
                        context.Response.Write(JsonConvert.SerializeObject(s.GetDataBaseTable()));
                    }
                    else
                    {
                        context.Response.Write(JsonConvert.SerializeObject(s.GetTableStructure(GetName)));
                    }
                }
              
                context.Response.Write("");
            }
            //添加
            if (Actioon == EnumAction.Add.ToString())
            {
                if (context.Request["DictionaryNameAdd"] != null && context.Request["GetUrl"] != null)
                {
                    mo.PlaceValue = context.Request["DictionaryNameAdd"].ToString();
                    var GetUrl = context.Request["GetUrl"].ToString();
                    var FirstGetTable = s.FindSingle(t => t.PlaceField == GetUrl);
                    mo.DictionaryName = FirstGetTable.DictionaryName;
                    mo.PlaceField = FirstGetTable.PlaceField;
                    mo.PlaceTable = FirstGetTable.PlaceTable;
                }
                else if (context.Request["DictionaryName"] != null && context.Request["PlaceTable"].ToString() != "0" && context.Request["PlaceField"].ToString() != "0")
                {
                    mo.DictionaryName = context.Request["DictionaryName"].ToString();
                    var PlaceTable = Convert.ToInt32(context.Request["PlaceTable"].ToString());
                    mo.PlaceTable = s.Find(t => t.PlaceId == PlaceTable).FirstOrDefault().PlaceValue;
                    mo.PlaceField = context.Request["PlaceField"].ToString();
                    mo.PlaceValue = context.Request["PlaceValue"].ToString();
                }
                context.Response.Write(s.Add(mo));
            }
            //修改
            if (Actioon == EnumAction.Update.ToString())
            {
                if (context.Request[""] != null)
                {

                }
                context.Response.Write(s.Update(mo));
            }
            //删除
            if (Actioon == EnumAction.Delete.ToString())
            {
                if (context.Request["GetId"] != null)
                {
                    var PlaceId = Convert.ToInt32(context.Request["GetId"].ToString());
                    var GetTable = s.Find(t => t.PlaceId == PlaceId).FirstOrDefault();
                    context.Response.Write(s.Delete(GetTable));
                }
            }
            context.Response.Write("");
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}